<?php
namespace app\common\controller;
class Sendmsg{
    public function sendmsg($phone,$con){
        $uid = '189893';		//数字用户名  59823  189893
        $pwd = '6e3sha';		//密码 xiong198412  6e3sha
        $mobile	 = $phone;	//号码
        $content = $con;		//内容
        //即时发送
        $res = $this->sendSMS($uid,$pwd,$mobile,$content);
        return $res;
    }
    //定时发送
    /*
    $time = '2010-05-27 12:11';
    $res = sendSMS($uid,$pwd,$mobile,$content,$time);
    echo $res;
    */
    public function sendSMS($uid,$pwd,$mobile,$content,$time='',$mid=''){
        $http = 'http://http.yunsms.cn/tx/?';
        $data = array(
            'uid'=>$uid,					//数字用户名
            'pwd'=>strtolower(md5($pwd)),	//MD5位32密码
            'mobile'=>$mobile,				//号码
            'content'=>$content,			//内容
            'content'=>iconv('utf-8','gbk',$content), //如果是utf-8编码，则需转码iconv('utf-8','gbk',$content); 如果是gbk则无需转码
            'time'=>$time,		//定时发送
            'mid'=>$mid						//子扩展号
            );
        $re= $this->postSMS($http,$data);			//POST方式提交
        return trim($re);
        if(trim($re) == '100'){
            return 1;
        }else{
            return "发送失败! 状态：".$re;
        }
    }
    
    function postSMS($url,$data=''){
        $post='';
        $row = parse_url($url);
        $host = $row['host'];
        $port = 80;
        $file = $row['path'];
        //while (list($k,$v) = each($data)) 
        foreach ($data as $k=>$v){
            $post .= rawurlencode($k)."=".rawurlencode($v)."&";	//转URL标准码
        }
        $post = substr( $post , 0 , -1 );
        $len = strlen($post);
        $fp = stream_socket_client("tcp://".$host.":".$port, $errno, $errstr, 10);

        if (!$fp) {
            return "$errstr ($errno)\n";
        } else {
            $receive = '';
            $out = "POST $file HTTP/1.0\r\n";
            $out .= "Host: $host\r\n";
            $out .= "Content-type: application/x-www-form-urlencoded\r\n";
            $out .= "Connection: Close\r\n";
            $out .= "Content-Length: $len\r\n\r\n";
            $out .= $post;		
            fwrite($fp, $out);
            while (!feof($fp)) {
                $receive .= fgets($fp, 128);
            }
            fclose($fp);
            $receive = explode("\r\n\r\n",$receive);
            unset($receive[0]);
            return implode("",$receive);
        }
    }
}